/**
 * date: 2011-04-25
 */
package com.epro.dx.util;

import java.util.HashMap;

import android.text.TextUtils;

import com.weipu.common.facade.model.OrderProductModel;
import com.weipu.common.facade.model.config.ConfigUtil;

/**
 * 程序全局配置
 *  1. 支付方式配置
 *  2. 用户信息配置
 *  3. SP信息配置
 *  4. 当前session
 * @author Administrator
 */
public final class GlobalConfig
{
    
    /**
     * config for paypal config
     */
    private PaypalCfg paypalConfig;
    
    /**
     * config for user
     */
    private UserConfig userConfig;
    
    //    /**
    //     * 首页广告图片信息
    //     */
    //    private List<BannerModel> bannerModels;
    
    /**
     * config for spCfg
     */
    private SpCfg spCfg;
    
    /**
     * config for current session
     */
    private SessionConfig sessionConfig;
    
    /**
     * 验证规则
     */
    private Validator validator;
    
    private boolean isShoppingCartChange = false;
    
    public boolean isShoppingCartChange()
    {
        return isShoppingCartChange;
    }
    
    public void setShoppingCartChange(boolean isShoppingCartChange)
    {
        this.isShoppingCartChange = isShoppingCartChange;
    }
    
    /**
     * 隐藏构造器，以便于全局只会有一个实例
     */
    private GlobalConfig()
    {
        paypalConfig = new PaypalCfg();
        userConfig = new UserConfig();
        spCfg = new SpCfg();
        sessionConfig = new SessionConfig();
        validator = new Validator();
        
        //默认验证规则
        //        validator.entity.setRuleContact("\\d{1,20}");
        validator.entity.setRuleContact("((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)");
        validator.entity.setRuleEmail("^(\\w|\\.|-|\\+)+@(\\w|-)+(\\.(\\w|-)+)+$");
        validator.entity.setRulePwd("\\S{5,20}");
        validator.entity.setRuleReceiver("\\S+");
        validator.entity.setRuleReceiverContact("\\d{1,20}");
        validator.entity.setRuleReceiverDetailAddress("\\S+");
        validator.entity.setRuleZipcode("\\d{1,20}");
    }
    
    /**
     * 全局配置单实例
     */
    private static GlobalConfig instance = new GlobalConfig();
    
    /**
     * 返回全局配置实例，通过该实例可以返回当前应用程序中其它配置
     * 1. Paypal 支付配置
     * 2. 当前会员配置
     * 3. 当前SP配置
     * 4. 当前session配置
     * 5. 验证规则
     * @return
     */
    public static GlobalConfig getInstance()
    {
        return instance;
    }
    
    /**
     * 数据验证配置
     * @author Administrator
     */
    public class Validator
    {
        private ValidatorEntity entity;
        
        Validator()
        {
            entity = new ValidatorEntity();
        }
        
        /**
         * 验证email规则
         * @param email
         * @return
         */
        public boolean validateEmail(String email)
        {
            return TextUtils.isEmpty(email) ? false : email.matches(entity.ruleEmail);
        }
        
        /**
         * 验证密码规则
         * @param pwd
         * @return
         */
        public boolean validatePwd(String pwd)
        {
            return TextUtils.isEmpty(pwd) ? false : pwd.matches(entity.rulePwd);
        }
        
        /**
         * 验证联系人规则
         * @param contact
         * @return
         */
        public boolean validateContact(String contact)
        {
            return TextUtils.isEmpty(contact) ? false : contact.matches(entity.ruleContact);
        }
        
        /**
         * 验证接收人规则
         * @param receiver
         * @return
         */
        public boolean validateReceiver(String receiver)
        {
            return TextUtils.isEmpty(receiver) ? false : receiver.matches(entity.ruleReceiver);
        }
        
        /**
         * 验证接收人联系方式规则
         * @param receiverContact
         * @return
         */
        public boolean validateReceiverContact(String receiverContact)
        {
            return TextUtils.isEmpty(receiverContact) ? false : receiverContact.matches(entity.ruleReceiverContact);
        }
        
        /**
         * 验证邮编规则
         * @param zipcode
         * @return
         */
        public boolean validateZipcode(String zipcode)
        {
            return TextUtils.isEmpty(zipcode) ? false : zipcode.matches(entity.ruleZipcode);
        }
        
        /**
         * 验证详细收货地址规则
         * @param receiverDetailAddress
         * @return
         */
        public boolean validateReceiverDetailAddress(String receiverDetailAddress)
        {
            return TextUtils.isEmpty(receiverDetailAddress) ? false
                : receiverDetailAddress.matches(entity.ruleReceiverDetailAddress);
        }
        
        public class ValidatorEntity
        {
            //            private ValidatorEntity()
            //            {
            //            }
            
            /**
             * 验证规则: email
             */
            private String ruleEmail;
            
            /**
             * 验证规则: 密码
             */
            private String rulePwd;
            
            /**
             * 验证规则: 联系方式
             */
            private String ruleContact;
            
            /**
             * 验证规则: 收货人
             */
            private String ruleReceiver;
            
            /**
             * 验证规则: 收货人联系方式
             */
            private String ruleReceiverContact;
            
            /**
             * 验证规则:  邮编
             */
            private String ruleZipcode;
            
            /**
             * 验证规则: 收货人详细地址
             */
            private String ruleReceiverDetailAddress;
            
            public String getRuleEmail()
            {
                return ruleEmail;
            }
            
            public void setRuleEmail(String ruleEmail)
            {
                this.ruleEmail = ruleEmail;
            }
            
            public String getRulePwd()
            {
                return rulePwd;
            }
            
            public void setRulePwd(String rulePwd)
            {
                this.rulePwd = rulePwd;
            }
            
            public String getRuleContact()
            {
                return ruleContact;
            }
            
            public void setRuleContact(String ruleContact)
            {
                this.ruleContact = ruleContact;
            }
            
            public String getRuleReceiver()
            {
                return ruleReceiver;
            }
            
            public void setRuleReceiver(String ruleReceiver)
            {
                this.ruleReceiver = ruleReceiver;
            }
            
            public String getRuleReceiverContact()
            {
                return ruleReceiverContact;
            }
            
            public void setRuleReceiverContact(String ruleReceiverContact)
            {
                this.ruleReceiverContact = ruleReceiverContact;
            }
            
            public String getRuleZipcode()
            {
                return ruleZipcode;
            }
            
            public void setRuleZipcode(String ruleZipcode)
            {
                this.ruleZipcode = ruleZipcode;
            }
            
            public String getRuleReceiverDetailAddress()
            {
                return ruleReceiverDetailAddress;
            }
            
            public void setRuleReceiverDetailAddress(String ruleReceiverDetailAddress)
            {
                this.ruleReceiverDetailAddress = ruleReceiverDetailAddress;
            }
        }
        
        public ValidatorEntity getEntity()
        {
            return entity;
        }
        
        public void setEntity(ValidatorEntity entity)
        {
            this.entity = entity;
        }
    }
    
    /**
     * config for sp
     * @author Administrator
     */
    public class SpCfg
    {
        /**
         * 鉴权标识，见{@link DotNetUtil#authenticate()}
         */
        private String uuid;
        
        /**
         * 是否展示运费
         */
        private boolean enableDisplayShipping;
        
        /**
         * 基本运费(没有达到最大优惠金额时的运费)
         */
        private double baseShipping;
        
        /**
         * 促销金额(达到该金额后可以免运费)
         */
        private double promotionAmount;
        
        /**
         * 是否允许促销(针对基本运费、促销金额)
         */
        private boolean enablePromotion = true;
        
        /**
         * currency
         */
        private String currency;
        
        /**
         * 位图缩放级别
         */
        private int bitmapSize = 4;
        
        /**
         * Paypal语言id
         * zh_CN 中文
         * en_US 英文
         */
        private String language;
        
        public String getLanguage()
        {
            return language;
        }
        
        public void setLanguage(String language)
        {
            this.language = language;
        }
        
        public String getUuid()
        {
            return uuid;
        }
        
        public void setUuid(String uuid)
        {
            this.uuid = uuid;
        }
        
        public String getCurrency()
        {
            return currency;
        }
        
        public void setCurrency(String currency)
        {
            this.currency = currency;
        }
        
        public double getBaseShipping()
        {
            return baseShipping;
        }
        
        public void setBaseShipping(double baseShipping)
        {
            this.baseShipping = baseShipping;
        }
        
        public double getPromotionAmount()
        {
            return promotionAmount;
        }
        
        public void setPromotionAmount(double promotionAmount)
        {
            this.promotionAmount = promotionAmount;
        }
        
        public void setEnablePromotion(boolean enablePromotion)
        {
            this.enablePromotion = enablePromotion;
        }
        
        public boolean isEnableDisplayShipping()
        {
            return enablePromotion ? enableDisplayShipping : false;
        }
        
        public void setEnableDisplayShipping(boolean enableDisplayShipping)
        {
            this.enableDisplayShipping = enableDisplayShipping;
        }
        
        public int getBitmapSize()
        {
            return bitmapSize;
        }
        
        public void setBitmapSize(int bitmapSize)
        {
            this.bitmapSize = bitmapSize;
        }
    }
    
    /**
     *
     * Config for paypal checkout
     * @author Administrator
     */
    public class PaypalCfg
    {
        /**
         * Paypal服务器运行环境，只能是以下值
         * @{link PayPal#ENV_LIVE}    生产环境
         * @{link PayPal#ENV_NONE}    无需连接到Paypal服务器
         * @{link PayPal#ENV_SANDBOX} 沙箱环境
         */
        private int serverEnvironment;
        
        /**
         * 应用程序ID,由Paypal分配，通过申请后获得(需要提交当前集成了Paypal支付功能的应用程序)
         */
        private String appId;
        
        /**
         * Paypal语言id
         * zh_CN 中文
         * en_US 英文
         */
        private String language;
        
        /**
         * 每次进行Paypal支付前，需要初始化Paypal(需要链接到Paypal服务器)，
         * 初始化完成Paypal返回一个设备标识
         */
        private String deviceToken;
        
        /**
         * 返回Paypal支付id
         * @return
         */
        public int getPaypalID()
        {
            return 1;
        }
        
        public String getPaypalMethod()
        {
            return "paypal";
        }
        
        PaypalCfg()
        {
        }
        
        public int getServerEnvironment()
        {
            return serverEnvironment;
        }
        
        public void setServerEnvironment(int serverEnvironment)
        {
            // 开发环境
            if (1 == ConfigUtil.getInstance().getConfig().getServerVersion())
            {
                return;
            }
            this.serverEnvironment = serverEnvironment;
        }
        
        public String getAppId()
        {
            return appId;
        }
        
        public void setAppId(String appId)
        {
            // 开发环境
            if (1 == ConfigUtil.getInstance().getConfig().getServerVersion())
            {
                return;
            }
            this.appId = appId;
        }
        
        public String getLanguage()
        {
            return language;
        }
        
        public void setLanguage(String language)
        {
            this.language = language;
        }
        
        public String getDeviceToken()
        {
            return deviceToken;
        }
        
        public void setDeviceToken(String deviceToken)
        {
            this.deviceToken = deviceToken;
        }
    }
    
    /**
     * Config for the user (Include user logined)
     * @author Administrator
     *
     */
    public class UserConfig
    {
        UserConfig()
        {
        }
        
        /**
         * 清除用户配置信息
         */
        public void clear()
        {
            uuid = null;
            email = null;
            password = null;
        }
        
        //用户唯一标识
        private String uuid;
        
        //email
        private String email;
        
        //密码
        private String password;
        
        //商户购物车支付地址
        private String checkoutPath;
        
        public String getUuid()
        {
            return uuid;
        }
        
        public void setUuid(String uuid)
        {
            this.uuid = uuid;
        }
        
        public String getEmail()
        {
            return email;
        }
        
        public void setEmail(String email)
        {
            this.email = email;
        }
        
        public String getPassword()
        {
            return password;
        }
        
        public void setPassword(String password)
        {
            this.password = password;
        }
        
        public String getCheckoutPath()
        {
            return checkoutPath;
        }
        
        public void setCheckoutPath(String checkoutPath)
        {
            this.checkoutPath = checkoutPath;
        }
        
    }
    
    /**
     * Config for current session
     * @author Administrator
     */
    public class SessionConfig
    {
        SessionConfig()
        {
            sessionCart = new SessionCart();
        }
        
        //界面布局中底部功能按钮选中索引
        //Home、Account、Browse、Cart、More
        private int menuSlectIndex;
        
        //保存底部功能按钮隐藏或显示状态
        private boolean isMenuHidden = false;
        
        //下单时产生的购物车对象
        private SessionCart sessionCart;
        
        public int getMenuSlectIndex()
        {
            return menuSlectIndex;
        }
        
        public void setMenuSlectIndex(int menuSlectIndex)
        {
            this.menuSlectIndex = menuSlectIndex;
        }
        
        /**
         * 清理session中缓存的数据以及状态
         */
        public void clear()
        {
            menuSlectIndex = 0;
            
            if (null != sessionCart)
            {
                sessionCart.clear();
                sessionCart = null;
            }
        }
        
        /**
         * 当前用户购物车会话，用户注销时需要通过
         *  {@link SessionConfig#clear()}来清理缓存数据
         * @author Administrator
         */
        public class SessionCart
        {
            private SessionCart()
            {
            }
            
            /**
             * 清理下单过程中产生的购物车Map对象
             */
            public void clear()
            {
                message = null;
                
                if (null != mycartMap)
                {
                    mycartMap.clear();
                    mycartMap = null;
                }
            }
            
            //用户留言
            private String message;
            
            //传递的参数为 购物车对象Map
            //以购物车ID为key，购物车对象为value的键值对
            private HashMap<Integer, OrderProductModel> mycartMap;
            
            public HashMap<Integer, OrderProductModel> getMycartMap()
            {
                return mycartMap;
            }
            
            public void setMycartMap(HashMap<Integer, OrderProductModel> mycartMap)
            {
                this.mycartMap = mycartMap;
            }
            
            public String getMessage()
            {
                return message;
            }
            
            public void setMessage(String message)
            {
                this.message = message;
            }
            
        }
        
        public SessionCart getSessionCart()
        {
            return sessionCart;
        }
        
        public void setSessionCart(SessionCart sessionCart)
        {
            this.sessionCart = sessionCart;
        }
        
        public boolean isMenuHidden()
        {
            return isMenuHidden;
        }
        
        public void setIsMenuHidden(boolean isMenuHidden)
        {
            this.isMenuHidden = isMenuHidden;
        }
        
    }
    
    //    public List<BannerModel> getBannerModels()
    //    {
    //        return bannerModels;
    //    }
    //    
    //    public void setBannerModels(List<BannerModel> bannerModels)
    //    {
    //        this.bannerModels = bannerModels;
    //    }
    
    public PaypalCfg getPaypalConfig()
    {
        return paypalConfig;
    }
    
    void setPaypalConfig(PaypalCfg paypalConfig)
    {
        this.paypalConfig = paypalConfig;
    }
    
    public UserConfig getUserConfig()
    {
        return userConfig;
    }
    
    void setUserConfig(UserConfig userConfig)
    {
        this.userConfig = userConfig;
    }
    
    public SpCfg getSpCfg()
    {
        return spCfg;
    }
    
    void setSpCfg(SpCfg spCfg)
    {
        this.spCfg = spCfg;
    }
    
    public SessionConfig getSessionConfig()
    {
        return sessionConfig;
    }
    
    public void setSessionConfig(SessionConfig sessionConfig)
    {
        this.sessionConfig = sessionConfig;
    }
    
    public Validator getValidator()
    {
        return validator;
    }
    
    public void setValidator(Validator validator)
    {
        this.validator = validator;
    }
}
